package org.kabeja.dxf.entity;

import org.kabeja.dxf.DXFDocument;

import java.util.*;

/**
 * @author shutao
 * @since 2022/4/24 17:51
 */
public class DXFLayer {
    private Map<String, List<DXFEntity>> entities = new HashMap<>();
    private String name = "";
    private int color = 7;
    private DXFDocument doc;
    private String ltype = "";
    private int flags = 0;
    private int lineWeight = 0;
    private String plotStyle = "";

    public DXFLayer() {
    }

    public String getName() {
        return this.name;
    }

    public void setName(String var1) {
        this.name = var1;
    }

    public void addDXFEntity(DXFEntity var1) {
        var1.setDXFDocument(this.doc);
        if (this.entities.containsKey(var1.getType())) {
            this.entities.get(var1.getType()).add(var1);
        } else {
            List<DXFEntity> var2 = new ArrayList<>();
            var2.add(var1);
            this.entities.put(var1.getType(), var2);
        }

    }

    public void removeDXFEntity(DXFEntity var1) {
        if (this.entities.containsKey(var1.getType())) {
            List<DXFEntity> var2 = this.entities.get(var1.getType());
            var2.remove(var1);
            if (var2.isEmpty()) {
                this.entities.remove(var1.getType());
            }
        }

    }

    public void setDXFDocument(DXFDocument var1) {
        this.doc = var1;
    }

    public DXFDocument getDXFDocument() {
        return this.doc;
    }

    public Bounds getBounds() {
        Bounds var1 = new Bounds();
        for (List<DXFEntity> dxfEntities : this.entities.values()) {
            for (DXFEntity var5 : dxfEntities) {
                Bounds var6 = var5.getBounds();
                if (var6.isValid()) {
                    var1.addToBounds(var6);
                }
            }
        }
        return var1;
    }

    public Bounds getBounds(boolean var1) {
        Bounds var2 = new Bounds();
        Iterator<List<DXFEntity>> iterator = this.entities.values().iterator();

        label38:
        while (iterator.hasNext()) {
            Iterator<DXFEntity> var5 = iterator.next().iterator();
            while(true) {
                DXFEntity var6;
                do {
                    if (!var5.hasNext()) {
                        continue label38;
                    }

                    var6 = var5.next();
                } while((!var1 || !var6.isModelSpace()) && (var1 || var6.isModelSpace()));

                Bounds var7 = var6.getBounds();
                if (var7.getMaximumX() == 0.0D / 0.0) {
                    System.out.println("NANA=" + var6);
                }
                if (var7.isValid()) {
                    var2.addToBounds(var7);
                }
            }
        }

        return var2;
    }

    public List<DXFEntity> getDXFEntities(String var1) {
        return this.entities.getOrDefault(var1, null);
    }

    public boolean hasDXFEntities(String var1) {
        return this.entities.containsKey(var1);
    }

    public Iterator<?> getDXFEntityTypeIterator() {
        return this.entities.keySet().iterator();
    }

    public DXFEntity getDXFEntityByID(String var1) {
        for (List<DXFEntity> dxfEntities : this.entities.values()) {
            for (DXFEntity var5 : dxfEntities) {
                if (var5.getID().equals(var1)) {
                    return var5;
                }
            }
        }
        return null;
    }

    public int getColor() {
        return this.color;
    }

    public void setColor(int var1) {
        this.color = var1;
    }

    public void setLineType(String var1) {
        this.ltype = var1;
    }

    public String getLineType() {
        return this.ltype;
    }

    public int getFlags() {
        return this.flags;
    }

    public void setFlags(int var1) {
        this.flags = var1;
    }

    public boolean isVisible() {
        return this.color >= 0;
    }

    public boolean isFrozen() {
        return (this.flags & 1) == 1;
    }

    public int getLineWeight() {
        return this.lineWeight;
    }

    public void setLineWeight(int var1) {
        this.lineWeight = var1;
    }

    public String getPlotStyle() {
        return this.plotStyle;
    }

    public void setPlotStyle(String var1) {
        this.plotStyle = var1;
    }
}
